package com.rimi.servlet;

import javax.servlet.ServletException;
import javax.servlet.annotation.MultipartConfig;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.Part;
import java.io.IOException;
import java.io.InputStream;

@WebServlet("/upload")
@MultipartConfig(location = "C:/Users/jerry/Desktop")
public class UploadServlet extends HttpServlet {


    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
    }

    @Override
    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        request.setCharacterEncoding("utf-8");
        String projectName = request.getParameter("projectName");
        String name = request.getParameter("name");

        Part part = request.getPart("file");
        //文件名
        String fileName=name+"---"+projectName;
        // 文件后缀
        String submittedFileName = part.getSubmittedFileName();
        int i = submittedFileName.lastIndexOf(".");
        String fileType = submittedFileName.substring(i);
        part.write("/项目资料/"+fileName+fileType);
        response.sendRedirect("/home.jsp");
    }

}
